import { createReducer, createAsyncAction ,createAction} from 'redux-action-tools';
let config = {
  authDomain: "kmxxg-chat.wilddog.com",
  syncURL: "https://kmxxg-chat.wilddogio.com"
};
wilddog.initializeApp(config);
let provider = new wilddog.auth.WeixinAuthProvider();

// ------------------------------------
// 
// ------------------------------------


const LOGIN = 'LOGIN',
  SETAUTH = 'SETAUTH';

export const LogIn = createAsyncAction(LOGIN, () => {
  return wilddog.auth().signInWithPopup(provider);
})

const ERROR = (state, action) => {
  console.log("请求失败了", action);
  return state
}

const authInfoHandler = (state, action) => {
  // console.log('检测到用户已登陆,设置权限信息',action.payload,action.payload.displayName);
  let user=action.payload||{};
  if('displayName' in user){
    let {displayName,uid,photoURL,providerId}=user;
    return Object.assign({}, state, 
      {status:true,displayName:displayName,uid:uid,photoURL:photoURL,providerId:providerId});

  }else{
    return Object.assign({}, state, 
      {status:false});

  }

}
export const SetAuth = createAction(SETAUTH)



const authReducer = createReducer()
  .when(LOGIN)
  .done(authInfoHandler)
  .when(SETAUTH,authInfoHandler)
  .failed(ERROR)
  .build({ status: false,info:'' });

export default authReducer



// export  const oneAnswerReducer= createReducer()
//  .when(GETALLTERMS)
//  .done(GETALLTERMS_SUCCESS)
//  .failed(getError)
//  .build({ answerslist: [] });
